



var Downit = {

    linksOnPage: null,
    imagesOnPage: null,
    xulOutputList: null,
    doc: null,
    whitelist: null, 

    gatherLinks: function()
    {
        this.linksOnPage = this.doc.links;
    },

    gatherImages: function()
    {
        this.imagesOnPage = this.doc.images;
    },

    // TODO need to make a strings util class 
    // and put these kinds of methods there.
    getFileExtension: function(filename)
    {
        // the path ends with a "/" 
        if(/\/$/.exec(filename))
        {
            return undefined;
        }

        if(/[.]/.exec(filename))
        {
            var fileExt = (/[^.]+$/.exec(filename)).toString();
            // if the file extension is long then 
            // its probably not file but a paramaters 
            if(fileExt.length > 5)
            {
               return undefined; 
            }
            return fileExt;
        }

        return undefined;
    },

    getFilename: function(url)
    {
        var filename = url.substring(url.lastIndexOf('/')+1);
        if(filename.length > 1)
        {
            return filename;
        }
        return "unknown";
    },

    loadValidExtFile: function(filePath)
    {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET",filePath,false);
        xmlhttp.send(null);
        var fileContent = xmlhttp.responseText;
        this.whitelist = fileContent.split(',');
        if((this.whitelist.length % 3) != 0)
        {
                alert("valid ext file might be corrupt.  comma miss-match ");
        }
        for(var i=0; i < this.whitelist.length; i+=3)
        {
            this.whitelist[i] = this.whitelist[i].replace("\n","");
            this.whitelist[i] = this.whitelist[i].replace("\r","");
            if(this.whitelist[i].length > 6)
            {
                alert("valid ext file might be corrupt around line: " + Math.floor((i+1)/3) + " (" + this.whitelist[i] + ")");
            }
        }
    },

    isValidMedia: function(fileExt)
    {
        if(fileExt == undefined) { return undefined; }

        for(var i=0; i < this.whitelist.length; i+=3)
        {
            if(this.whitelist[i].toLowerCase() == fileExt.toLowerCase()) 
            {
                //alert(this.whitelist[i] + " " + fileExt); 
                return i;
            }
        }

        return undefined;
    },

    populatedXul: function()
    {
        // TODO These need to be in their own methods.
        if(this.linksOnPage != null)
        {
            for (var i = 0; i < this.linksOnPage.length; i++)
            {
                var xulRow = document.createElement("row"); 
                var mediaName = document.createElement("listcell");
                var mediaType = document.createElement("listcell");

                var fileExt = "";
                var filename = "";
                filename = this.getFilename(this.linksOnPage[i].href);
                fileExt = this.getFileExtension(this.linksOnPage[i].href);
                validFile = this.isValidMedia(fileExt);
                if(validFile != undefined)
                {
                        mediaType.setAttribute("label", this.whitelist[validFile+1] + " ");
                        mediaName.setAttribute("label", filename);
                        xulRow.appendChild(mediaType);
                        xulRow.appendChild(mediaName);

                        this.xulOutputList.appendChild(xulRow);
                }
            }
        }

        if(this.imagesOnPage != null)
        {
            for (var i = 0; i < this.imagesOnPage.length; i++)
            {
                var xulRow = document.createElement("row");
                var mediaName = document.createElement("listcell");
                var mediaType = document.createElement("listcell");

                mediaType.setAttribute("label", "image ");
                mediaName.setAttribute("label", this.imagesOnPage[i].src);

                xulRow.appendChild(mediaType);
                xulRow.appendChild(mediaName);

                this.xulOutputList.appendChild(xulRow);
            }
        }
    },

    onLoad: function()
    {
        var win = Components.classes['@mozilla.org/appshell/window-mediator;1']
            .getService(Components.interfaces.nsIWindowMediator)
            .getMostRecentWindow('navigator:browser');
        var browser = win.gBrowser.mCurrentBrowser;
        this.doc = browser.contentDocument;
        // There is probably a better way of checking to see
        // if the current page is "valid"
        if(this.doc.links == undefined)
        {
            // TODO better error message
            alert ("This page cannot be parsed");
        }

        // havnt figured out how to filter not wanted images
        // this.gatherImages();

        this.loadValidExtFile("chrome://Downit/content/validext.txt");

        this.gatherLinks();
        this.xulOutputList = document.getElementById("list-of-media"); 
        this.populatedXul();
   }
};

